using System;

namespace Infinity.Common.Logger
{
	/// <summary>
	/// Logger class for Console
	/// </summary>
	public class ConsoleLogger : TimeLoggerBase
	{
		private static ConsoleLogger _Instance;

		private ConsoleLogger()
		{
		}

		private void Write(ConsoleColor color, string text, bool logTime = true)
		{
			ConsoleColor foregroundColor = Console.ForegroundColor;
			Console.ForegroundColor = color;

			Console.WriteLine(logTime ?ToLogTime(text) : text);

			Console.ForegroundColor = foregroundColor;
		}

		private void WriteLine(ConsoleColor color, string text, bool logTime = true)
		{
			Write(color, text + Environment.NewLine, logTime);
		}

		public override void Flush()
		{
			Console.Clear();
		}

		public override void Alert(string text)
		{
			WriteLine(ConsoleColor.DarkRed, "Alert: " + text);
		}

		public override void Info(string text)
		{
			WriteLine(ConsoleColor.Green, "Info: " + text);
		}

		public override void Warning(string text)
		{
			WriteLine(ConsoleColor.Yellow, "Warning: " + text);
		}

		public override void Error(string text)
		{
			WriteLine(ConsoleColor.Red, "Error: " + text);
		}

		protected override void Write(string text, bool logTime)
		{
			Console.Write(logTime ?ToLogTime(text) : text);
		}

		public static ConsoleLogger Instance
		{
			get
			{
				if (_Instance == null)
					_Instance = new ConsoleLogger();
				return _Instance;
			}
		}
	}
}